﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.world {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.world.vos.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.models.world.vos.areas.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.models.world.vos.actors.machines.*;
    import net.wooga.pets.models.world.vos.actors.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;

    public class PatientMediator extends AbstractActorMediator {

        public var sunModel:SunModel;
        private var story:MovieClip;

        private function get view():PatientView{
            return ((viewComponent as PatientView));
        }
        private function get vo():PatientVO{
            return ((_vo as PatientVO));
        }
        override public function onRegister():void{
            super.onRegister();
            eventMap.mapListener(eventDispatcher, EventConstants.DISABLE_PET_VIEW, this.disablePetView);
            eventMap.mapListener(eventDispatcher, EventConstants.ENABLE_PET_VIEW, this.enabledPetView);
            if (this.vo.hasTreatmentsFinished){
                this.updateActor(null);
            };
        }
        override protected function updateRenderList(_arg1:Event=null):void{
        }
        override protected function updateRenderListForDragging(_arg1:Event=null):void{
        }
        override protected function updateActor(_arg1:Object):void{
            super.updateActor(_arg1);
            this.updateVisibility();
        }
        override protected function setPosition(_arg1:Number, _arg2:Number):void{
            this.view.direction = ((_arg1)>=this.view.x) ? -1 : 1;
            super.setPosition(_arg1, _arg2);
        }
        override protected function onMouseOut(_arg1:MouseEvent):void{
            if (this.vo.state != WorldConstants.ILL){
                dispatch(new InfoEvent(EventConstants.PATIENT_SPEECH_BUBBLE_UPDATE, id));
            };
            super.onMouseOut(_arg1);
        }
        override protected function onMouseOver(_arg1:MouseEvent):void{
            if (this.vo.state != WorldConstants.ILL){
                this.vo.showSpeechBubble(SpeechBubbleConstants.TYPE_CUSTOM_BUBBLE, {treatments:this.vo.treatments});
            };
            super.onMouseOver(_arg1);
        }
        override protected function onClick(_arg1:MouseEvent):void{
            var _local2:String = worldModel.getRoomIdsOfType(GameConstants.TYPE_ENTRANCE_AREA)[0];
            if (this.vo.roomId != _local2){
                dispatch(new InfoEvent(EventConstants.ON_PATIENT_CLICKED, id));
            };
        }
        override protected function setIconBubble():void{
            super.setIconBubble();
            eventMap.unmapListener(eventDispatcher, EventConstants.TICK_PAUSED, this.onTickPaused);
            eventMap.unmapListener(eventDispatcher, EventConstants.TICK_STARTED, this.onTickStarted);
        }
        override protected function setCustomBubble():void{
            var _local2:MovieClip;
            var _local1:ISpeaking = (_vo as ISpeaking);
            if (this.vo.currentTreatment){
                _local2 = AssetsLibrary.getMovieClip(SpeechBubbleConstants.PET_CUSTOM_BUBBLE);
                this.fillBubbleWithTreatments(_local2);
                _local2.x = (_local2.x - SpeechBubbleConstants.PET_BUBBLE_OFFSET_LEFT);
                this.view.speechBubblePosition = _local1.speechBubblePosition;
                this.view.addSpeechBubble(_local2);
            };
        }
        override protected function setStoryBubble():void{
            var receptionMachineVO:ReceptionMachineVO;
            var bubble:MovieClip;
            var speaker:ISpeaking = (_vo as ISpeaking);
            var patient:ITreatable = (_vo as ITreatable);
            var reception:RoomVO = RoomVO(worldModel.getRoomOfActor(patient.id));
            receptionMachineVO = ReceptionMachineVO(reception.machine);
            bubble = AssetsLibrary.getMovieClip(SpeechBubbleConstants.STORY_BUBBLE);
            var storyName:String = ((SpeechBubbleConstants.STORY + patient.type) + patient.diseaseName);
            this.story = AssetsLibrary.getMovieClip(storyName);
            this.story.addFrameScript((this.story.totalFrames - 1), function ():void{
                story.stop();
                bubble.StoryContainer.removeChild(story);
                story = null;
                receptionMachineVO.storyOfSicknessAnimationComplete(vo.id);
            });
            eventMap.mapListener(eventDispatcher, EventConstants.TICK_PAUSED, this.onTickPaused);
            eventMap.mapListener(eventDispatcher, EventConstants.TICK_STARTED, this.onTickStarted);
            bubble.StoryContainer.addChild(this.story);
            this.view.speechBubblePosition = speaker.speechBubblePosition;
            this.view.addSpeechBubble(bubble);
        }
        private function onTickPaused(_arg1:Event):void{
            if (this.story){
                this.story.stop();
            };
        }
        private function onTickStarted(_arg1:Event):void{
            if (this.story){
                this.story.play();
            };
        }
        override protected function showView():void{
            dispatch(new InfoEvent(EventConstants.SET_PATIENT, null, this.vo));
        }
        override protected function hideView():void{
            Displays.removeChild(this.view);
        }
        override protected function startMoving():void{
            if (this.vo.canWalk){
                super.startMoving();
            };
        }
        private function fillBubbleWithTreatments(_arg1:MovieClip):void{
            var _local5:TreatmentVO;
            var _local6:MovieClip;
            var _local7:MovieClip;
            var _local8:MovieClip;
            var _local9:ColorTransform;
            var _local2:MovieClip = new MovieClip();
            var _local3:Number = 0;
            var _local4:int;
            while (_local4 < this.vo.treatments.length) {
                _local5 = TreatmentVO(this.vo.treatments[_local4]);
                _local6 = AssetsLibrary.getMovieClip(("Logo" + _local5.roomType));
                if (_local5 == this.vo.currentTreatment){
                    _local8 = AssetsLibrary.getMovieClip("ProgressArrow");
                    _local8.x = _local3;
                    _local2.addChild(_local8);
                    _local3 = (_local3 + (_local8.width + SpeechBubbleConstants.PET_BUBBLE_ICON_PADDING));
                } else {
                    if (_local5.isFinalized){
                        _local9 = new ColorTransform();
                        new ColorTransform().redOffset = (new ColorTransform().greenOffset = (_local9.blueOffset = -100));
                        _local6.transform.colorTransform = _local9;
                        _local7 = AssetsLibrary.getMovieClip("Tick");
                        _local7.x = _local3;
                    };
                };
                _local6.x = _local3;
                _local3 = (_local3 + (_local6.width + SpeechBubbleConstants.PET_BUBBLE_ICON_PADDING));
                _local2.addChild(_local6);
                if (_local7){
                    _local2.addChild(_local7);
                };
                _local4++;
            };
            _arg1.background.width = ((SpeechBubbleConstants.PET_BUBBLE_MARGIN_LEFT + _local2.width) + SpeechBubbleConstants.PET_BUBBLE_MARGIN_RIGHT);
            _arg1.background.height = (((SpeechBubbleConstants.PET_BUBBLE_MARGIN_TOP + _local2.height) + SpeechBubbleConstants.PET_BUBBLE_MARGIN_BOTTOM) + SpeechBubbleConstants.PET_BUBBLE_OFFSET_BOTTOM);
            _local2.x = SpeechBubbleConstants.PET_BUBBLE_MARGIN_LEFT;
            _local2.y = (-(_arg1.height) + SpeechBubbleConstants.PET_BUBBLE_MARGIN_TOP);
            _arg1.addChild(_local2);
        }
        private function enabledPetView(_arg1:InfoEvent):void{
            if (_arg1.id == this.vo.id){
                this.view.setEnabled();
                eventMap.mapListener(this.view, MouseEvent.CLICK, this.onClick);
                eventMap.mapListener(this.view, MouseEvent.MOUSE_OVER, this.onMouseOver);
                eventMap.mapListener(this.view, MouseEvent.MOUSE_OUT, this.onMouseOut);
            };
        }
        private function disablePetView(_arg1:InfoEvent):void{
            if (_arg1.id == this.vo.id){
                this.view.setDisabled();
                eventMap.unmapListener(this.view, MouseEvent.CLICK, this.onClick);
                eventMap.unmapListener(this.view, MouseEvent.MOUSE_OVER, this.onMouseOver);
                eventMap.unmapListener(this.view, MouseEvent.MOUSE_OUT, this.onMouseOut);
            };
        }
        override protected function toggleFacade(_arg1:BuildingEvent=null):void{
            this.updateVisibility();
        }
        private function updateVisibility():void{
            var _local1:Boolean = (this.vo.isVisible) ? !(gameStateModel.isFacadeVisible) : false;
            if (_local1){
                this.view.show();
            } else {
                this.view.hide();
            };
        }

    }
}//package net.wooga.pets.views.world 
